<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/12/09 06:59:32 $)
// $RCSfile: admin_restoretemplates.php,v $ : $Revision: 1.1 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}


if (!$staffauth->isAdmin()) {
	trigger_error("You do not have permission to access $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");
require_once ("./includes/functions_template.php");
$template->loadLanguageSection("templates");
?><script language="Javascript" src="<?php echo $_SWIFT["themepath"]; ?>main.js"></script><?

//echo "Restore Templates THIS is test";

function restoreTemplates($templateid)
{
	global $dbCore;
	$_updatetemplte = array();
	if(!_is_array($templateid))
	{
		return false;
	}
	
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."templates` AS templates LEFT JOIN `". TABLE_PREFIX ."templatedata` AS templatedata ON (templates.templateid = templatedata.templateid) WHERE templates.templateid IN (". buildIN($templateid) .");");
	while ($dbCore->nextRecord())
	{
		$_updatetemplte[] = $dbCore->Record["templateid"];
	}

	if (empty($_updatetemplte))
	{
		return false;
	}

	// Ok now update the db to original contents

	$dbCore->shutdownQuery("UPDATE `". TABLE_PREFIX ."templates` SET `contentshash` = '". md5($_template["contentsdefault"]) ."', `dateline` = '". DATENOW ."', `modified` = '". MODIFIED_NONE ."', `templatelength` = '". strlen($_template["contentsdefault"]) ."' WHERE `templateid` IN (". buildIN($_updatetemplte) .");");

	$dbCore->shutdownQuery("UPDATE `". TABLE_PREFIX ."templatedata` SET `contents` = `contentsdefault` WHERE `templateid`IN (". buildIN($_updatetemplte) .");");

	return count($_updatetemplte);	
}


if ($eventaction == "restoretemplates")
{
	$interface->adminHeader($_SWIFT["language"]["templates"]." > ".$_SWIFT["language"]["restoretemplates"], 1);

	$interface->adminNavBar('<a href="index.php?_m=core&_a=dashboard" title="'.$_SWIFT["language"]["dashboard"].'">'.$_SWIFT["language"]["dashboard"].'</a> &raquo; <a href="index.php?_m=core&_a=managetemplates" title="'.$_SWIFT["language"]["templates"].'">'.$_SWIFT["language"]["templates"].'</a> &raquo; <a href="index.php?_m=core&_a=restoretemplates" title="'.$_SWIFT["language"]["restoretemplates"].'">'.$_SWIFT["language"]["restoretemplates"].'</a>', $_SWIFT["language"]["desc_templates"] , 0);
	
	printInfoBox($infomessage);
	printErrorBox($errormessage);
	
		
	if ($_POST["listmodified"] == 1)
	{
		
		$_modify = $_POST["modified"];
		$name = "selecttemplate";

		// List all modified templates
		printMainTableHeader(sprintf($_SWIFT["language"]["listtemplates"], $_SWIFT["tgroupcache"][$_REQUEST["tgroupid"]]["title"]));

		printFormStart($name, "index.php", "POST", false, "", "");

		$colarray[0]["align"]="center";
		$colarray[0]["nowrap"] = true;
		$colarray[0]["value"] = '<input type="checkbox" name="allselect" class="swiftcheckbox" onClick="javascript:toggleAll(document.'.$name.');">';
		$colarray[0]["width"] = 20;

		$colarray[1]["align"] = "left";
		$colarray[1]["nowrap"] = true;
		$colarray[1]["value"] = $_SWIFT["language"]["templatename"];

		$colarray[2]["align"] = "center";
		$colarray[2]["nowrap"] = true;
		$colarray[2]["value"] = $_SWIFT["language"]["status"];
		printDataRow($colarray, "tabletitlerow");

		$dbCore->query("SELECT `templateid`, `name`, `modified` FROM `". TABLE_PREFIX ."templates` WHERE `tgroupid` = '". intval($_POST["tgroupid"]) ."' AND `modified`IN (". buildIN($_modify) .") ORDER BY `templateid` ASC;");
	
		while ($dbCore->nextRecord())
		{
			if ($dbCore->Record["modified"] == MODIFIED_NONE) {
				$modifiedstatus = "icon_templatenotmodified.gif";
				$modifiedtext = $_SWIFT["language"]["notmodified"];
			} else if ($dbCore->Record["modified"] == MODIFIED_UPGRADE) {
				$modifiedstatus = "icon_templateupgrade.gif";
				$modifiedtext = $_SWIFT["language"]["upgrade"];
			} else {
				$modifiedstatus = "icon_templatemodified.gif";
				$modifiedtext = $_SWIFT["language"]["modified"];
			}
				$id = $dbCore->Record["templateid"];
				//echo "Template ID:" . $dbCore->Record["templateid"]."<br>";
				$colarray = array();
				$colarray[0]["align"]="center";
				$colarray[0]["nowrap"] = true;
				$colarray[0]["value"] = '<input type="checkbox" name="itemid[]" value='.$id.' class=\"swiftcheckbox\" >';
				$colarray[0]["width"] = 20;

				$colarray[1]["align"] = "left";
				$colarray[1]["nowrap"] = true;
				$colarray[1]["value"] = '<a href="'. $_SWIFT["swiftpath"] .'admin/index.php?_m=core&_a=edittemplate&templateid='. $dbCore->Record["templateid"] .'&tgroupid='. intval($_POST["tgroupid"]) .'">'.$dbCore->Record["name"]."</a>";


				$colarray[2]["align"] = "center";
				$colarray[2]["nowrap"] = true;
				$colarray[2]["value"] = '<img src="'. $_SWIFT["themepath"].$modifiedstatus .'" border="0" />&nbsp;'.$modifiedtext .'';
				$colarray[2]["width"] = 250;

				printDataRow($colarray, getRowBG());
		}

		printSubmitRow($_SWIFT["language"]["restore"], "3");
		printMainTableFooter();

		addHiddenField("_m", "core");
		addHiddenField("_a", "restoretemplates");
		addHiddenField("type", "restore");

		printFormEnd();

		$template->assign("backurl", "index.php?_m=core&_a=templatediagnostics");

	} else {
		// ======= LIST MODIFIED TEMPLATES =======
			if($_POST["type"] == "restore")
			{

		$resrestore = restoreTemplates($_POST["itemid"]);
		if($resrestore){
			$infomessage = sprintf($_SWIFT["language"]["templatesrestoreconfirm"], $resrestore);
		    }
		}

		printInfoBox($infomessage);
		printErrorBox($errormessage);

		printFormStart("modifiedform", "index.php", "POST", true);
		printMainTableHeader($_SWIFT["language"]["listtorestore"]);

		$index = 0;
		foreach ($_SWIFT["tgroupcache"] as $key=>$val)
		{
			$options[$index]["title"] = $val["title"];
			$options[$index]["value"] = $val["tgroupid"];
			$index++;
		}

		printSelectRow("tgroupid", $_SWIFT["language"]["moditgroup"], $_SWIFT["language"]["desc_moditgroup"], $options);
		
		echo '<tr class="'. getRowBG() .'"><td><img src="'. $_SWIFT["themepath"] .'icon_templatemodified.gif" border="0" />&nbsp;<strong>'. $_SWIFT["language"]["modified"] .'</strong></td><td><input type="checkbox" name="modified[]" value="'.MODIFIED_USER.'" checked></td></tr>';
		
		echo '<tr class="'. getRowBG() .'"><td><img src="'. $_SWIFT["themepath"] .'icon_templateupgrade.gif" border="0" />&nbsp;<strong>'. $_SWIFT["language"]["upgrade"] .'</strong></td><td><input type="checkbox" name="modified[]" value="'.MODIFIED_UPGRADE.'"></td></tr>';
		
		echo '<tr class="'. getRowBG() .'"><td><img src="'. $_SWIFT["themepath"] .'icon_templatenotmodified.gif" border="0" />&nbsp;<strong>'. $_SWIFT["language"]["notmodified"] .'</strong></td><td><input type="checkbox" name="modified[]" value="'.MODIFIED_NONE.'"></td></tr>';
		
		printSubmitRow($_SWIFT["language"]["list"]);

		printMainTableFooter();

		addHiddenField("_m", "core");
		addHiddenField("_a", "restoretemplates");
		addHiddenField("listmodified", "1");
		printFormEnd();

		$template->assign("backurl", "index.php?_m=core&_a=managetemplates");
	}

	
	$interface->adminFooter();
}
?>